package com.autumn.mybatis.provider.mysql.builder;

import com.autumn.mybatis.metadata.EntityTable;
import com.autumn.mybatis.provider.builder.AbstractUpdateBuilder;
import com.autumn.mybatis.provider.mysql.MySqlProvider;
import com.autumn.mybatis.provider.mysql.MySqlUtils;
import com.autumn.mybatis.provider.util.MybatisSqlUtils;

/**
 * Mysql 更新生成器
 * 
 * @author 杨昌国
 *
 *         2017-10-19 08:35:21
 */
public class MySqlUpdateBuilder extends AbstractUpdateBuilder {

	/**
	 * 
	 * @param dbProvider
	 */
	public MySqlUpdateBuilder(MySqlProvider dbProvider) {
		super(dbProvider);
	}

	@Override
	public String getUpdateMapSql(EntityTable table, String paramName) {
		StringBuilder sql = new StringBuilder();
		sql.append("UPDATE ");
		sql.append(MybatisSqlUtils.getTableOrViewName(table));
		sql.append("<set>");
		sql.append("<foreach collection=\"" + paramName + ".updateClauses\" item=\"item\" separator=\",\">");
		sql.append("${item.columnName} = #{item.value}");
		sql.append("</foreach>");
		sql.append("</set>");
		sql.append(MySqlUtils.createSectionWhereSql(paramName));
		return sql.toString();
	}
}
